package com.stu.management.controller;


import com.stu.management.pojo.Grade;
import com.stu.management.pojo.Student;
import com.stu.management.service.GradeService;
import com.stu.management.service.impl.StudentServiceImpl;
import com.stu.management.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class StudentController {
    @Resource(name = "studentServiceImpl")
    private StudentServiceImpl studentService;
    @Resource(name = "gradeServiceImpl")
    GradeService gradeService;
    @Autowired
    private PageUtil pageUtil;

    @RequestMapping("/student/StudentManagement.do")
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println("开始执行学生管理");
        String method = req.getParameter("method");
        if("insert".equals(method)){
            insert(req,resp);
        }else if("update".equals(method)){
            update(req, resp);
        }else if("findbyid".equals(method)){
            findbyid(req, resp);
        }else if("delete".equals(method)){
            deletestu(req, resp);
        }else{
            findlist(req, resp);
        }
    }

    private void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        //根据主键查询学生信息
        Student student = studentService.findById(Integer.parseInt(sid));

        //查询年级列表

        List<Grade> list = gradeService.getList();

        req.setAttribute("glist",list);
        req.setAttribute("stu",student);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    private void deletestu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sid = req.getParameter("sid");
        //根据主键查询学生信息
        int i = studentService.delStu(sid);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/4StudentManagement/student/StudentManagement'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/4StudentManagement/student/StudentManagement'</script>");
        }
    }

    private void findlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.1 模糊查条件
        String stuname = req.getParameter("stuname");
        System.out.println(stuname);
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //1.2 分页数据   limit 开始位置,显示条数
        //页码值(当前页码值)
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageIndex的值，则认为默认查询第一页
        int index = (pageIndex == null)?1:Integer.parseInt(pageIndex);


        int usersex = (null==sex||0==sex.length()?-1:Integer.parseInt(sex));
//        调用service
        List<Student> students = studentService.getStudents(stuname, stuno, usersex, index, pageUtil.getPageSize());
        int total = studentService.total(stuname, stuno, usersex);
        System.out.println("总条数： "+total);
        pageUtil.setTotal(total);
        //3.跳转页面
        //后台想给前台传数据，在后台存值
        pageUtil.setDataList(students);
        pageUtil.setPageIndex(index);
        //存储模糊查条件
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",sex);
        //存储分页数据
        req.setAttribute("p1",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sid = req.getParameter("sid");
        String stuNo = req.getParameter("stuNo");
        String stuname = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introdction = req.getParameter("introdction");

        //步骤2
        Student stu = new Student();
        stu.setStuname(stuname);
        stu.setAddress(address);
        stu.setSex(Integer.parseInt(sex));
        stu.setStuid(Integer.parseInt(sid));

        System.out.println(stu.toString());

        int i = studentService.updateStu(stu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('更新成功');location.href='/4StudentManagement/student/StudentManagement'</script>");
        }else{
            writer.println("<script>alert('更新失败');location.href='/4StudentManagement/student/StudentManagement?method=findbyid&sid="+sid+"'</script>");
        }
    }

    private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String stuNo = req.getParameter("stuNo");
        String stuname = req.getParameter("stuName");
        System.out.println(stuname);
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introdction = req.getParameter("introdction");
        //调取service
        //将参数封装到学生对象中
        Student student = new Student();
        student.setStuno(stuNo);
        student.setStuname(stuname);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdnumber(idnumber);
        student.setProfession(profession);
        student.setIntroduction(introdction);
        System.out.println(student.toString());
        int i = studentService.insertStu(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('新增成功');location.href='/4StudentManagement/student/StudentManagement'</script>");
        }else{
            writer.println("<script>alert('新增失败');location.href='/4StudentManagement/student/getGradeList'</script>");
        }
    }
}
